package org.elsfs.cloud.common.mybatis.ext.sql.support.group;


import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.util.lang.NamingCase;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 标准分组实现
 *

 */
@RequiredArgsConstructor
public class StandardGroupBy implements GroupByItem {

    /**
     * 字段名称
     */
    private final Collection<String> fields;

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        return String.format(
            "GROUP BY %s",
            this.fields.stream()
                .map(NamingCase::toUnderlineCase)
                .collect(Collectors.joining(", "))
        );
    }
}
